package org.ycx.doc.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.ycx.common.constants.RespCode;
import org.ycx.common.dto.RespMSG;
import org.ycx.doc.dao.enums.GroupUsersRole;
import org.ycx.doc.dao.model.DocProject;
import org.ycx.doc.dao.model.DocUserVersion;
import org.ycx.doc.dto.DocProjectInfo;
import org.ycx.doc.dto.DocUser;
import org.ycx.doc.service.IDocProjectService;
import org.ycx.doc.service.IProfileService;
import org.ycx.upms.common.constants.SecurityConstants;
import org.ycx.upms.dao.model.SecUser;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/profile")
public class ProfileController {

    private String PRE = "profile/";

    @Autowired
    private IProfileService service;

    @Autowired
    private IDocProjectService projectService;

    @RequestMapping("/index.html")
    public String indexPage(ModelMap mp) {
        return PRE + "index";
    }

    @RequestMapping("/setting.html")
    public String settingPage(ModelMap mp) {
        return PRE + "settings";
    }


    @RequestMapping("/updatePwd.html")
    public String updatePwdPage(ModelMap mp) {
        return PRE + "updatePwd";
    }

    @RequestMapping("/initData")
    @ResponseBody
    public RespMSG initData(HttpSession session) {
        RespMSG resp = new RespMSG ();
        int uid = (int) session.getAttribute (SecurityConstants.CURRENT_UID);
        DocUser user = this.service.findUserProfile (uid);
        resp.initResp (RespCode.SUCCESS);
        resp.setData (user);
        return resp;
    }


    @RequestMapping("/settingData")
    @ResponseBody
    public RespMSG settingData(HttpSession session) {
        RespMSG resp = new RespMSG ();
        int uid = (int) session.getAttribute (SecurityConstants.CURRENT_UID);
        List<DocProjectInfo> user = this.service.findUserProject (uid);
        resp.initResp (RespCode.SUCCESS);
        resp.setData (user);
        return resp;
    }

    @RequestMapping("/addUserProject.html")
    public String addUserProjectPage(ModelMap modelMap, HttpSession session) {
        int uid = (int) session.getAttribute (SecurityConstants.CURRENT_UID);
        List<GroupUsersRole> roles = new ArrayList<GroupUsersRole> ();
        roles.add (GroupUsersRole.Master);
        roles.add (GroupUsersRole.Developer);
        roles.add (GroupUsersRole.Guest);
        List<DocProject> projects = this.projectService.findProjectByUser (uid, roles);
        modelMap.put ("projects", projects);
        return PRE + "addUserProject";
    }

    @RequestMapping("/updateUserApp/{appId}.html")
    public String updateUserAppPage(ModelMap modelMap, @PathVariable("appId") Integer appId, HttpSession session) {
        int uid = (int) session.getAttribute (SecurityConstants.CURRENT_UID);
        List<DocProjectInfo> projects = this.service.findUserProject (uid);
        for (DocProjectInfo project : projects) {
            if (project.getAppId ().equals (appId)) {
                modelMap.put ("project", project);
                break;
            }
        }
        return PRE + "updateUserProject";
    }


    @RequestMapping("/updateUserApp")
    @ResponseBody
    public RespMSG updateUserApp(Integer appId, Integer userVersion, HttpSession session) {
        RespMSG resp = new RespMSG ();
        int uid = (int) session.getAttribute (SecurityConstants.CURRENT_UID);
        int res = this.service.updateUserProject (appId, uid, userVersion);
        resp.initResp (RespCode.SUCCESS);
        resp.setData (res);
        return resp;
    }

    @RequestMapping("/projects")
    @ResponseBody
    public RespMSG projects(ModelMap modelMap, HttpSession session) {
        RespMSG resp = new RespMSG ();
        int uid = (int) session.getAttribute (SecurityConstants.CURRENT_UID);
        List<GroupUsersRole> roles = new ArrayList<GroupUsersRole> ();
        roles.add (GroupUsersRole.Master);
        roles.add (GroupUsersRole.Developer);
        roles.add (GroupUsersRole.Guest);
        List<DocProject> projects = this.projectService.findProjectByUser (uid, roles);
        modelMap.put ("projects", projects);
        resp.initResp (RespCode.SUCCESS);
        resp.setData (projects);
        return resp;
    }

    @RequestMapping("/saveUserApp")
    @ResponseBody
    public RespMSG saveUserApp(Integer appId, Integer userVersion, HttpSession session) {
        RespMSG resp = new RespMSG ();
        int uid = (int) session.getAttribute (SecurityConstants.CURRENT_UID);
        DocUserVersion duv = new DocUserVersion ();
        duv.setAppId (appId);
        duv.setDevVersion (userVersion);
        duv.setUserId (uid);
        DocProjectInfo project = this.service.addUserVersion (duv);
        project.setUserVersion (userVersion);
        resp.initResp (RespCode.SUCCESS);
        resp.setData (project);
        return resp;

    }


    @RequestMapping("/updateProfile")
    @ResponseBody
    public RespMSG updateProfile(DocUser user, HttpSession session) {
        RespMSG resp = new RespMSG ();
        int uid = (int) session.getAttribute (SecurityConstants.CURRENT_UID);
        user.setUid (uid);
        int res = this.service.updateUser (user);
        if (res > 0) {
            resp.initResp (RespCode.SUCCESS);
            resp.setData (user);
        } else {
            resp.initResp (RespCode.FAIL);
        }
        return resp;
    }

    @RequestMapping("/delUserApp/{id}")
    @ResponseBody
    public RespMSG delUserApp(@PathVariable("id") Integer appId, HttpSession session) {
        RespMSG resp = new RespMSG ();
        int uid = (int) session.getAttribute (SecurityConstants.CURRENT_UID);
        int res = this.service.delUserApp (appId, uid);
        if (res > 0) {
            resp.initResp (RespCode.SUCCESS);
        } else {
            resp.initResp (RespCode.FAIL);
        }
        return resp;
    }

    @RequestMapping("/updatePwd")
    @ResponseBody
    public RespMSG updatePwd(String oldPwd, String savePwd) {
        RespMSG respMSG = new RespMSG ();
        //TODO 修改密码
        respMSG.initResp (RespCode.SUCCESS);
        return respMSG;
    }
}
